﻿s<script>

    var _vm;
    $(function () {
        var vm = new ViewModel();
        _vm = vm;
        ko.applyBindings(vm);
        vm.Init();
    });

    function ViewModel() {
        var me = this;
        ViewModelBase.call(me);
        vmExtend.call(me);

        me.DeleteUrl = "@this.ContentUrl("~/ArticleManage/Article/Delete")";
        me.Dialog = new Dialog(me);
        ////////////////////////////////
        //详情页
        me.DetailDialog = new DetailDialog(me);
        me.Detail = function () {
            me.DetailDialog.Open(me.DataTable.SelectedModel(), "详情");
        }
        ////////////////////////////////
    }
    function vmExtend() {
        var me = this;

        me.Init = function () {
            me.LoadModels();
        }

        me.LoadModels = function (page) {
            me.DataTable.SelectedModel(null);
            var params = me.SearchModel();
            params.page = page || 1;
            $ace.get("@this.ContentUrl("~/ArticleManage/Article/GetModels")", params, function (result) {
                me.DataTable.SetPagedData(result.Data);
            }
          );
        }
    }
    //////////////////////////////
    //详情页对话框设置
    function DetailDialog() {
        var me = this;
        DialogBase.call(me);

        me.OnOpen = function () {

            me.ShowSaveButton(false);
            var model = me.EditModel();
            if (model) {
                var bindModel = $ko.toJS(model);
                me.Model(bindModel);
            }
            else {
                me.Model({ IsEnabled: true });
            }
            //选中的动作ID
            var actionId = me.Model().id;

            $.ajax({
                url: "/ArticleManage/Article/Details",
                async: false,
                success: function (data) {
                    location.href = "/ArticleManage/Article/Details?keyValue=" + actionId;
                }
            });
        }
    }

    ///////////////////////////////
    function Dialog(vm) {
        var me = this;
        DialogBase.call(me);


        me.OnOpen = function () {

            me.IsShow(false);
            var model = me.EditModel();
            if (model) {
                var bindModel = $ko.toJS(model);
                me.Model(bindModel);
            }
            else {
                me.Model({ IsEnabled: true });
            }
            //选中的动作ID
            var actionId = me.Model().id;

            $.ajax({
                url: "/ArticleManage/Article/Form",
                async: false,
                success: function (data) {
                    location.href = "/ArticleManage/Article/Form?keyValue=" + actionId;
                }
            });
        }

        me.OnSave = function () {
            var model = me.Model();

            if (!$('#form1').formValid()) {
                return false;
            }

            if (me.EditModel()) {
                $ace.post("@this.ContentUrl("~/ArticleManage/Article/Update")", model, function (result) {
                    $ace.msg(result.Msg);
                    me.Close();
                    vm.LoadModels();
                }
               );
            }
        }
    }
</script>
